home *** CD-ROM | disk | FTP | other *** search
/ ShareWare OnLine 2 / ShareWare OnLine Volume 2 (CMS Software)(1993).iso / prog / suprman.zip / EXAMPLE.PRG < prev    next >
Text File  |  1993-04-26  |  8KB  |  281 lines

  1. /****
  2. *   Program Name: FIX.PRG
  3. *
  4. *   Date Created: 03/23/93
  5. *   Time Created: 09:59:15
  6. *   Author      : Michael Abadjiev  CIS: 71563,3312
  7. *   Language    : Clipper 5.20
  8. *   Compile     : clipper FIX.PRG /n /w
  9. *
  10. *   -=- NOTE: Suggestions greatly appreciated - Mike
  11. */
  12.  
  13. #include "inkey.ch"
  14. #include "fileio.ch"
  15.  
  16. #define B_SOLID_SPACE                  '█▀███▄██ '
  17.  
  18. FUNCTION Fix(cFileIn,cFileOut)
  19.  
  20.     LOCAL nFileSize := 0,               ;
  21.     nOccure := 0,                 ;
  22.     getlist := {},                ;
  23.     nHandle,                      ;
  24.     nMaxLen := 64,                ;
  25.     cBuffer,                      ;
  26.     nPtrPos := 0,                 ;
  27.     cTemp := "",                  ;
  28.     nSearch,                      ;
  29.     nReadBytes := 0,              ;
  30.     lRename := .f.,               ;
  31.     lEdit := .f.
  32.  
  33.     set confirm on
  34.     set scoreboard off
  35.  
  36.     BEGIN SEQUENCE
  37.         // Dealing with parameters....
  38.         IF pcount() == 0
  39.             cFileIn := space(13)
  40.             cFileOut := space(13)
  41.             lEdit := .t.
  42.         ELSEIF pcount() == 1
  43.             IF !file(cFileIn)
  44.                 alert("ERROR: File " + upper(cFileIn) + " is not a valid name!")
  45.                 BREAK
  46.             ENDIF
  47.             cFileOut := "_TEMP_"
  48.             lRename := .t.
  49.         ELSEIF pcount() == 2
  50.             IF !file(cFileIn)
  51.                 alert("ERROR: File " + upper(cFileIn) + " not found!")
  52.                 cFileIn := pad(cFileIn,13)
  53.                 cFileOut := pad(cFileOut,13)
  54.                 lEdit := .t.
  55.             ENDIF
  56.         ENDIF
  57.  
  58. /*
  59. $SUPERMAN$
  60. $CHAPTER$
  61. 2
  62. $SUBCHAPTER$
  63. 1
  64. $HEADING$
  65. Command Line Parameters
  66. $TXT$
  67. You can bypass the input/output file screen by using the built-in command
  68. line parameters.
  69.  
  70. Here is the usage:
  71.  
  72.     FIX Infile OutFile [enter]
  73.  
  74. $END$
  75. */
  76.         // Some drawing....
  77.         dispbegin()
  78.         scroll()
  79.         dispbox( 02,01,07,maxcol()-1,B_SOLID_SPACE,"BG+/B")
  80.         @ 03,02 say padc("Program to replace all occurrences",76) color "GR+/B"
  81.         @ 04,02 say padc("of....:  CLIPPER501", 76) color "W+/B"
  82.         @ 05,02 say padc("with..:  CLIPPER520", 76) color "W+/B"
  83.         @ 06,02 say padc("Written by: Michael Abadjiev CIS: 71563,3312",76)  ;
  84.         color "B+/B"
  85.         dispend()
  86. /*
  87. $SUPERMAN$
  88. $CHAPTER$
  89. 0
  90. $SUBCHAPTER$
  91. 0
  92. $HEADING$
  93. What is FIX.PRG
  94. $TXT$
  95.  
  96. The Fix program will replace all occurrences
  97.  
  98.             of....:  CLIPPER501
  99.  
  100.             with..:  CLIPPER520
  101.  
  102. This is so you don't have to recompile all of your clipper libraries.
  103.  
  104. Fix was written by: Michael Abadjiev CIS: 71563,3312
  105.  
  106. $END$
  107. */
  108.         // Shall we have GETs.....
  109.         IF lEdit
  110.             @ maxrow(),0 say padc("Press <Esc> to Exit",maxcol()+1) color "W/B"
  111.             devpos(maxrow()-5,00)
  112.             @ row(),00  say "Input Library file name......:"   ;
  113.             get cFileIn                            ;
  114.             picture "@!"                           ;
  115.             valid file(cFileIn)
  116. /*
  117. $SUPERMAN$
  118. $CHAPTER$
  119. 1
  120. $SUBCHAPTER$
  121. 1
  122. $HEADING$
  123. Input Library Name
  124. $TXT$
  125. If you don't use the command line parameters, you will have to enter the
  126. name of the input library you wish to change.  
  127.  
  128. Simple type the library name and press [enter].
  129. $END$
  130. */
  131.  
  132.             @ row()+1,00 say "Output Library file name.....:"  ;
  133.             get cFileOut                          ;
  134.             picture "@!"                          ;
  135.             valid !file(cFileOut)
  136. /*
  137. $SUPERMAN$
  138. $CHAPTER$
  139. 1
  140. $SUBCHAPTER$
  141. 2
  142. $HEADING$
  143. OUTPUT Library Name
  144. $TXT$
  145. If you don't use the command line parameters, you will have to enter the
  146. name of the output library you wish the input library to be saved to.  
  147.  
  148. Simple type the library name and press [enter].
  149. $END$
  150. */
  151.             read
  152.             @ maxrow(),0
  153.  
  154.             // Exit...
  155.             IF lastkey() == K_ESC
  156. /*
  157. $SUPERMAN$
  158. $CHAPTER$
  159. 1
  160. $SUBCHAPTER$
  161. 3
  162. $HEADING$
  163. <ESC> Key
  164. $TXT$
  165. If you wish to abbort this program, press the [esc] Key whan it asks you 
  166. for the input or output filenames.
  167. $END$
  168. */
  169.                 scroll()
  170.                 BREAK
  171.             ENDIF
  172.         ENDIF
  173.  
  174.         // Make a second copy of the library and work with this copy...
  175.         __copyfile(cFileIn,cFileOut)
  176.         // Low level file stuff...
  177.         nHandle := fopen(cFileOut,FO_READWRITE)
  178.         IF ferror() <> 0
  179.             alert("ERROR: Opening file!")
  180.             BREAK
  181.         END
  182.  
  183.         // Get the file size....
  184.         nFileSize := FileSize(nHandle)
  185.         @ maxrow(),0 say "File Size...: " + alltrim(transform(nFileSize,"99,999,999"))
  186.  
  187.         WHILE .t.
  188.             // Display the search - progress...
  189.             nPtrPos := FilePos( nHandle )
  190.             @ maxrow(),maxcol()-30 say "Searching........:" + str(nPtrPos,10)
  191.  
  192.             cTemp := ""
  193.             cBuffer := space(64)
  194.             // Read 64 bytes....
  195.             nReadBytes := fread(nHandle,@cBuffer,nMaxLen)
  196.             // Look how many we have red...
  197.             // in case less then 64 bytes we are very close to EOF...
  198.             // actually this is the last shot...
  199.             IF nReadBytest <> nMaxLen
  200.                 fseek(nHandle,-nReadBytes, FS_RELATIVE)
  201.                 cBuffer := left(cBuffer,nReadBytes)
  202.                 nSearch := at("CLIPPER501",cBuffer)
  203.                 // If found...
  204.                 IF nSearch > 0
  205.                     cTemp := strtran(cBuffer,"CLIPPER501","CLIPPER520")
  206.                     fwrite(nHandle,cTemp,nReadBytes)
  207.                     nOccure++
  208.                     @ maxrow()-2, 00 say                                ;
  209.                     padc(" Replace CLIPPER501->CLIPPER520 Times: "+     ;
  210.                     alltrim(transform(nOccure,"99,999")) ,maxcol()+1)   ;
  211.                     color "GR+/N"
  212.                 ENDIF
  213.                 EXIT
  214.             ENDIF
  215.  
  216.             // Otherwise we are somewhere in the file...
  217.             nSearch := at("CLIPPER501",cBuffer)
  218.             // If found...
  219.             IF nSearch <> 0
  220.                 // go back
  221.                 fseek(nHandle,-nMaxLen,FS_RELATIVE)
  222.                 // change the buffer..
  223.                 cTemp := strtran(cBuffer,"CLIPPER501","CLIPPER520")
  224.                 // write the new contents...
  225.                 fwrite(nHandle,cTemp,nMaxLen)
  226.                 cTemp := ""
  227.                 // reasign the buffer just for sure...
  228.                 cBuffer := space(64)
  229.                 // increment the counter...
  230.                 nOccure++
  231.                 // some screen output...
  232.                 @ maxrow()-2, 00 say                                ;
  233.                 padc(" Replace CLIPPER501->CLIPPER520 Times: "+     ;
  234.                 alltrim(transform(nOccure,"99,999")) ,maxcol()+1)   ;
  235.                 color "GR+/N"
  236.             ENDIF
  237.             // tricky go back lenght of 'CLIPPER501' + something
  238.             // just to be sure that we don't missed something...
  239.             fseek(nHandle,-12,FS_RELATIVE)
  240.             // do the same thing again....
  241.         END
  242.  
  243.         fclose(nHandle)
  244.         IF lRename
  245.             ferase(cFileIn)
  246.             frename(cFileOut,cFileIn)
  247.         ENDIF
  248.         @ maxrow(),0 say "Occurences.....: " + str(nOccure,10)
  249.         // Beep... That's it!
  250.         tone(7000,10)
  251.  
  252.     END SEQUENCE
  253.  
  254. RETURN nil
  255.  
  256.  
  257. STATIC FUNCTION FileSize( nHandle )
  258.  
  259.     LOCAL nCurrent
  260.     LOCAL nLength
  261.  
  262.     // Get file position
  263.     nCurrent := FilePos( nHandle )
  264.  
  265.     // Get file length
  266.     nLength := fseek( nHandle, 0, FS_END )
  267.  
  268.     // Reset file position
  269.     fseek( nHandle, nCurrent )
  270.  
  271. RETURN nLength
  272.  
  273.  
  274. STATIC FUNCTION FilePos( nHandle )
  275.  
  276. RETURN fseek( nHandle, 0, FS_RELATIVE )
  277.  
  278.  
  279. // **** EOF: fix.prg
  280. 
  281.